/*! \file $Id: MotionSettingUI.java 35266 2014-08-07 07:17:57Z litl $
 * \author 作者
 * \brief 手势说明提示
 */
package com.bnq.control.desktop.popup;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bnq.control.R;
import com.bnq.control.common.utils.DisplayUtils;


public class MotionPopup extends XmlPopup {

    private ViewPager mVp_motionsettiong;
    private static int[] motions = new int[]{R.drawable.virtual_mouse,
            R.drawable.touch_motion};
    private LinearLayout mLl_pointers;

    public MotionPopup(Context context) {
        super(context, R.layout.motionsetting);
        setBackgroundDrawable(context.getResources().getDrawable(
                R.drawable.bg_repeat));
        String country = context.getResources().getConfiguration().locale
                .getCountry();
        if (country.equals("CN") || country.equals("TW")) {
            motions = new int[]{R.drawable.virtual_mouse_cn,
                    R.drawable.touch_motion_cn};
        }
    }

    @Override
    protected void onInitView(View rootView, OnClickListener l) {
        super.onInitView(rootView, l);
        Button btnClose = (Button) rootView.findViewById(R.id.btnClose);
        btnClose.setOnClickListener(l);
        mVp_motionsettiong = (ViewPager) rootView
                .findViewById(R.id.vp_motionsettiong);
        mVp_motionsettiong.setAdapter(new MotionPagerAdapter());
        mLl_pointers = (LinearLayout) rootView.findViewById(R.id.ll_pointers);
        mLl_pointers.removeAllViews();
        for (int i = 0; i < motions.length; i++) {
            ImageView iv = new ImageView(getContext());
            iv.setImageDrawable(getContext().getResources().getDrawable(
                    R.drawable.desktop_menu_more_motionpopup_pointer));
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);

            if (i == 0) {
                iv.setEnabled(true);
                params.rightMargin = DisplayUtils.dp2px(10, getContext());
                iv.setLayoutParams(params);
            } else {
                iv.setEnabled(false);
            }
            mLl_pointers.addView(iv);
        }
        mVp_motionsettiong.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                for (int i = 0; i < motions.length; i++) {
                    mLl_pointers.getChildAt(i).setEnabled(arg0 == i);
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    class MotionPagerAdapter extends PagerAdapter {
        MotionPagerAdapter() {
        }

        @Override
        public int getCount() {
            return motions.length;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(getContext());
            imageView.setImageResource(motions[position]);
            container.addView(imageView);
            return imageView;
        }

    }
}
